package com.tbycoder.tby.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tbycoder.tby.entity.IposDiscussionPublisher;
import com.tbycoder.tby.entity.IposDiscussionReturner;
import com.tbycoder.tby.entity.Request.GetContentRequest;
import com.tbycoder.tby.entity.Request.PageRequest;
import com.tbycoder.tby.entity.Request.PublishRequest;
import com.tbycoder.tby.entity.Vo.GetTopicContextVo;
import com.tbycoder.tby.excption.BizException;
import com.tbycoder.tby.mapper.IposDiscussionPublisherMapper;
import com.tbycoder.tby.mapper.IposDiscussionReturnerMapper;
import com.tbycoder.tby.service.IIposDiscussionPublisherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbycoder.tby.utils.TokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tby
 * @since 2021-05-13
 */
@Service
public class IposDiscussionPublisherServiceImpl extends ServiceImpl<IposDiscussionPublisherMapper, IposDiscussionPublisher> implements IIposDiscussionPublisherService {

    @Resource
    IposDiscussionPublisherMapper iposDiscussionPublisherMapper;
    @Resource
    IposDiscussionReturnerMapper iposDiscussionReturnerMapper;
    
    @Override
    public void publish(PublishRequest publishRequest) {
        String userId = TokenUtil.getUserId();
        if(publishRequest.getTopic().equals("")){
            throw new BizException("未填写主题，请填写");
        }
        if(publishRequest.getContext().equals("")){
            throw new BizException("未填写内容，请填写");
        }
        
        iposDiscussionPublisherMapper.insert(
                IposDiscussionPublisher.builder()
                        .publisherId(userId)
                        .createTime(LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()))
                        .publisherContent(publishRequest.getTopic())
                        .build()
        );
        iposDiscussionReturnerMapper.insert(
                IposDiscussionReturner.builder()
                        .createTime(LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()))
                        .publisherId(userId)
                        .publisherContent(publishRequest.getTopic())
                        .returnerId(userId)
                        .returnerContent(publishRequest.getContext())
                        .build()
        );
    }

    @Override
    public Page<GetTopicContextVo> getTopic(PageRequest pageRequest) {
        Page<IposDiscussionPublisher> iposDiscussionPublisherPage = new Page<>();

        iposDiscussionPublisherPage.setCurrent(pageRequest.getCurrentPage());
        iposDiscussionPublisherPage.setSize(pageRequest.getPageSize());

        Page<GetTopicContextVo> getTopicContextVoPage = new Page<>();
        List<GetTopicContextVo> getTopicContextVos = new ArrayList<>();

        List<IposDiscussionPublisher> records = iposDiscussionPublisherMapper.selectPage(iposDiscussionPublisherPage,
                Wrappers.lambdaQuery(IposDiscussionPublisher.class).orderByDesc(IposDiscussionPublisher::getCreateTime)).getRecords();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for(IposDiscussionPublisher iposDiscussionPublisher : records){
            GetTopicContextVo getTopicContextVo = new GetTopicContextVo();
            getTopicContextVo.setPublishUserId(iposDiscussionPublisher.getPublisherId());
            getTopicContextVo.setTopic(iposDiscussionPublisher.getPublisherContent());
            getTopicContextVo.setPublishTime(df.format(iposDiscussionPublisher.getCreateTime()));
            List<IposDiscussionReturner> iposDiscussionReturners = iposDiscussionReturnerMapper.selectList(Wrappers.lambdaQuery(IposDiscussionReturner.class)
                    .eq(IposDiscussionReturner::getPublisherId, iposDiscussionPublisher.getPublisherId()));
//            List<String> returnContext = new ArrayList<>();
//            List<String> returnId = new ArrayList<>();
//            for(IposDiscussionReturner iposDiscussionReturner : iposDiscussionReturners){
//                returnContext.add(iposDiscussionReturner.getReturnerContent());
//                returnId.add(iposDiscussionReturner.getReturnerId());
//            }
//            getTopicContextVo.setContext(returnContext);
//            getTopicContextVo.setReturnUserId(returnId);
            getTopicContextVo.setCount(iposDiscussionReturners.size()-1);
            getTopicContextVos.add(getTopicContextVo);
        }
        getTopicContextVoPage.setRecords(getTopicContextVos);
        return getTopicContextVoPage;
    }

    @Override
    public GetTopicContextVo getContext(GetContentRequest getContentRequest) {
        Page<IposDiscussionReturner> iposDiscussionReturnerPage = new Page<>();
        iposDiscussionReturnerPage.setCurrent(0);
        iposDiscussionReturnerPage.setSize(10);
            List<IposDiscussionReturner> iposDiscussionReturners = iposDiscussionReturnerMapper.selectPage(iposDiscussionReturnerPage,Wrappers.lambdaQuery(IposDiscussionReturner.class)
            .eq(IposDiscussionReturner::getPublisherId,getContentRequest.getPublisherId())
            .eq(IposDiscussionReturner::getPublisherContent,getContentRequest.getTopic())).getRecords();
            List<String> returnContext = new ArrayList<>();
            List<String> returnId = new ArrayList<>();
            for(IposDiscussionReturner iposDiscussionReturner : iposDiscussionReturners){
                if(!(iposDiscussionReturner.getReturnerId().equals(getContentRequest.getPublisherId())&&iposDiscussionReturner.getReturnerContent().equals(""))) {
                    System.out.println(iposDiscussionReturner.getReturnerContent()+iposDiscussionReturner.getReturnerId()+getContentRequest.getPublisherId());
                    returnContext.add(iposDiscussionReturner.getReturnerContent());
                    returnId.add(iposDiscussionReturner.getReturnerId());
                }
            }
            GetTopicContextVo getTopicContextVo =new GetTopicContextVo();
            getTopicContextVo.setContext(returnContext);
            getTopicContextVo.setReturnUserId(returnId);
        System.out.println(getTopicContextVo);
        return getTopicContextVo;
    }

    @Override
    public void returnContent(GetContentRequest getContentRequest) {
        String userId = TokenUtil.getUserId();
        if(getContentRequest.getReturnContent() == null){
            throw new BizException("内容不能为空");
        }
        iposDiscussionReturnerMapper.insert(
                IposDiscussionReturner.builder()
                        .returnerContent(getContentRequest.getReturnContent())
                        .returnerId(userId)
                        .publisherContent(getContentRequest.getTopic())
                        .publisherId(getContentRequest.getPublisherId())
                        .createTime(LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()))
                        .build()
        );
    }

    @Override
    public void deleteTopic(GetContentRequest getContentRequest) {
        iposDiscussionPublisherMapper.delete(Wrappers.lambdaQuery(IposDiscussionPublisher.class)
        .eq(IposDiscussionPublisher::getPublisherId,getContentRequest.getPublisherId())
        .eq(IposDiscussionPublisher::getPublisherContent,getContentRequest.getTopic()));
    }


}
